﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.IO;
using ExamAnalysis.Core;
using ExamAnalysis.Model;


namespace ExamAnalysis.Input
{
    public partial class InputClassInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnUpload_DirectClick(object sender, Ext.Net.DirectEventArgs e)
        {
            string UploadFile = "";

            if (this.FileField.HasFile)
            {
                UploadFile = this.FileField.PostedFile.FileName.ToString();
                int FileSize = Int32.Parse(this.FileField.PostedFile.ContentLength.ToString());
                if (FileSize > 5 * 1024 * 1024)
                {
                    X.Msg.Alert("提示信息", "上传文件过大！").Show();
                    return;
                }
                string strFileName = Path.GetExtension(this.FileField.PostedFile.FileName).ToLower();//获取文件后缀
                if (!(strFileName == ".xls" || strFileName == ".xlsx"))
                {
                    X.Msg.Alert("提示信息", "文件格式不正确！").Show();
                    return;
                }

                Random ran = new Random();
                string sNewName = DateTime.Now.ToString(@"yyyyMMddHHmmss") + ran.Next(100, 999)
                  + Path.GetExtension(this.FileField.PostedFile.FileName);
                string strPath = Server.MapPath("~/FileUpload/" + sNewName);
                if (!Directory.Exists(Path.GetDirectoryName(strPath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(strPath));
                }
                this.FileField.PostedFile.SaveAs(strPath);
                //
                FileInfo file = new FileInfo(strPath);
                X.Msg.Alert("提示信息", String.Format("成功添加{0}个班级", InputUtil.inputClassFromExcel(file, 2).ToString())).Show();

                file.Delete();
            }
            else
            {
                X.Msg.Alert("提示信息", "请选择您要上传的文件!").Show();
            }
        }

    }
}